<template>
  <div>
    <div v-if="loading" class="loading-indicator">加载中...</div>
    <div v-else class="file-list-container">
      <!-- 添加表头 -->
      <div class="file-list-header">
        <div class="header-name" @click="sortBy('name')">
          名称
          <span v-if="sortField === 'name'" class="sort-icon">
              {{ sortOrder === 'asc' ? '↑' : '↓' }}
            </span>
        </div>
        <div class="header-size" @click="sortBy('size')">
          大小
          <span v-if="sortField === 'size'" class="sort-icon">
              {{ sortOrder === 'asc' ? '↑' : '↓' }}
            </span>
        </div>
        <div class="header-date" @click="sortBy('lastModified')">
          修改时间
          <span v-if="sortField === 'lastModified'" class="sort-icon">
              {{ sortOrder === 'asc' ? '↑' : '↓' }}
            </span>
        </div>
      </div>

      <ul class="file-list">
        <!-- 上一级目录项 -->
        <li
            v-if="canNavigateUp"
            :class="{'drag-over': dragOverParentFolder}"
            class="is-folder"
            @dblclick="navigateUp"
            @dragover.prevent="handleParentFolderDragOver"
            @dragleave.prevent="handleParentFolderDragLeave"
            @drop.prevent="handleParentFolderDrop"
        >
                <span class="icon">
                  <img
                      :src="folderIcon"
                      alt="parent folder"
                      class="folder-icon"
                  >
                </span>
          <span class="name"> ↩  ... </span>
          <span class="size"></span>
          <span class="date"></span>
        </li>

        <!-- 普通文件和文件夹项 -->
        <li
            v-for="item in sortedItems"
            v-if="sortedItems.length > 0"
            :key="item.path"
            :class="{
              'is-folder': item.folderFlag,
              'is-file': !item.folderFlag,
              'selected': selectedItem?.path === item.path,
              'drag-over': dragOverItem === item.path
            }"
            :draggable="!item.folderFlag"
            @click="handleItemClick(item)"
            @dblclick="handleItemDblClick(item)"
            @dragend="handleDragEnd"
            @dragstart="handleDragStart(item, $event)"
            @dragover.prevent="handleDragOver(item)"
            @dragleave.prevent="handleDragLeave(item)"
            @drop.prevent="handleDrop(item)"
        >
            <span class="icon">
              <img
                  v-if="item.folderFlag"
                  :src="folderIcon"
                  alt="folder"
                  class="folder-icon"
              >
              <img
                  v-else
                  :src="getFileIcon(item.name)"
                  alt="file"
                  class="file-icon"
              >
            </span>
          <span class="name">{{ item.name }}</span>
          <span v-if="!item.folderFlag" class="size">{{ item.size > 0 ? formatFileSize(item.size) : '' }}</span>
          <span v-if="!item.folderFlag" class="date">{{ formatDate(item) }}</span>
        </li>

      </ul>
      <div v-if="sortedItems.length == 0" class="empty-folder">
        当前文件夹为空
      </div>
    </div>
  </div>

</template>

<script lang="ts" setup>

import {
  canNavigateUp,
  type FileItem,
  formatDate,
  formatFileSize,
  getFileIcon,
  handleDrop,
  handleParentFolderDrop, loading,
  navigateUp,
  previewCurrentFileItem,
  selectedItem,
  sortBy, sortedItems,
  sortField,
  sortOrder, updateCurrentPathFileList,
  updateSpecifyFolderDirectoryFileList
} from "@/views/documentManager/components/ts/FileBrowser";
import {
  dragOverItem,
  dragOverParentFolder, handleDragEnd, handleDragLeave, handleDragOver, handleDragStart,
  handleParentFolderDragLeave,
  handleParentFolderDragOver
} from "@/views/documentManager/components/ts/dragItem";
import folderIcon from "@/asserts/documentTypeSvg/folder2.svg";
import {onMounted} from "vue";



// 单击处理当个文件或者文件夹条目
const handleItemClick = (item: FileItem) => {
  selectedItem.value = item;
};


// 双击处理当个文件或者文件夹条目
const handleItemDblClick = (item: FileItem) => {
  if (item.folderFlag) {
    enterFolder(item);
  } else {
    previewCurrentFileItem();
  }
};

// 进入文件夹
const enterFolder = async (item: FileItem) => {
  if (!item.folderFlag) return;

  await updateSpecifyFolderDirectoryFileList(item.path);
};

// 初始化从根目录加载文件
onMounted(async () => {
  await updateCurrentPathFileList()
});


</script>


<style lang="scss" scoped>

.file-list-header {
  display: flex;
  padding: 12px 16px;
  background-color: #f5f5f5;
  border-radius: 4px 4px 0 0;
  font-weight: bold;
  user-select: none;
}

.file-list-header > div {
  cursor: pointer;
  display: flex;
  align-items: center;
}

/* 文件列表容器 */
.file-list-container {
  flex-grow: 1;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
}

/* 文件列表表头 */
.file-list-header {
  display: flex;
  padding: 10px 16px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #e0e0e0;
  font-weight: bold;
  user-select: none;
}

.file-list-header > div {
  cursor: pointer;
  display: flex;
  align-items: center;
}

.file-list-header > div:hover {
  color: #4CAF50;
}

.header-name {
  flex: 1;
  min-width: 0; /* 防止flex item溢出 */
}

.header-size {
  width: 100px;
  text-align: right;
  margin-right: 10px;
  flex-shrink: 0;
}

.header-date {
  width: 180px;
  text-align: right;
  flex-shrink: 0;
}

.sort-icon {
  margin-left: 4px;
  font-size: 12px;
}

/* 文件列表 */
.file-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 600px;
  overflow-y: auto;
}

/* 文件列表项 */
.file-list li {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid #e0e0e0;
  cursor: pointer;
  user-select: none;
  transition: background-color 0.2s;
}

.file-list li:last-child {
  border-bottom: none;
}

.file-list li.drag-over {
  background-color: #e6f7ff;
  border: 1px dashed #1890ff;
}

.file-list li.is-file:active {
  opacity: 0.5;
  cursor: grabbing;
}

.file-list li:hover {
  background-color: #f5f5f5;
}

.file-list li.selected {
  background-color: #e3f2fd;
}

/* 列表项内容样式 - 与表头严格对齐 */
.file-list li .icon {
  margin-right: 8px;
  font-size: 18px;
  flex-shrink: 0;
  width: 20px;
}

.file-list li .name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-list li .size {
  width: 100px;
  text-align: right;
  margin-right: 10px;
  font-size: 12px;
  color: #666;
  flex-shrink: 0;
}

.file-list li .date {
  width: 180px;
  text-align: right;
  font-size: 12px;
  color: #666;
  white-space: nowrap;
  flex-shrink: 0;
}

.content-wrapper {
  display: flex;
  gap: 20px;
  margin-top: 16px;
}

.loading-indicator,
.empty-folder {
  padding: 20px;
  text-align: center;
  color: #666;
}

.is-folder {
  font-weight: bold;
}

.is-file {
  font-weight: normal;
}

.action-panel {
  width: 250px;
  padding: 16px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  background-color: #f9f9f9;
  position: sticky;
  top: 20px;
  height: fit-content;
}

.action-panel h3 {
  margin-top: 0;
  padding-bottom: 8px;
  border-bottom: 1px solid #e0e0e0;
  color: #333;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.action-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  width: 100%;
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 4px;
  transition: all 0.2s;
  cursor: pointer;
  text-align: left;
}

.selected-item-actions {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #e0e0e0;
}

.no-selection-message {
  margin-top: 12px;
  padding: 10px;
  text-align: center;
  color: #666;
  font-size: 12px;
  background-color: #f5f5f5;
  border-radius: 4px;
  transition: all 0.2s;
}

.action-button:hover {
  border-color: #4CAF50;
  color: #4CAF50;
  background-color: #f0f0f0;
  transform: translateY(-1px);
}

.action-button .icon {
  font-size: 16px;
  margin-right: 8px;
}

.action-button.danger {
  color: #f44336;
  border-color: #f44336;
}

.action-button.danger:hover {
  background-color: #ffebee;
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}

.toolbar-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  gap: 10px;
}

.search-container {
  display: flex;
  align-items: center;
}

.search-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-input {
  padding: 8px 12px 8px 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  width: 250px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='16' height='16' stroke='%23666' stroke-width='2' fill='none' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='8'%3E%3C/circle%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'%3E%3C/line%3E%3C/svg%3E");
  background-position: 8px center;
  background-repeat: no-repeat;
}

/* 搜索框 高亮*/
.search-input:focus {
  outline: none;
  border-color: #4CAF50;
  width: 250px;
}

.clear-search-button {
  position: absolute;
  right: 4px;
  background: none;
  border: none;
  cursor: pointer;
  color: #666;
  font-size: 18px;
  line-height: 1;
  padding: 4px;
}

.clear-search-button:hover {
  color: #333;
}

.left-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.toolbar-button {
  padding: 8px 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.2s;
}

.toolbar-fresh-button {
  padding: 8px 16px;
  background-color: #13ce66;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.2s;
}

.toolbar-fresh-button:hover {
  background-color: #45a049;
}

.toolbar-button:hover {
  background-color: #45a049;
}

.navigation {
  display: flex;
  align-items: center;
  padding: 8px;
  background-color: #f5f5f5;
  border-radius: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-grow: 1;
  min-width: 0;
}

.home-button {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  margin-right: 8px;
  padding: 4px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.home-button:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.home-icon {
  width: 20px;
  height: 20px;
  vertical-align: middle;
}

.back-button {
  margin-right: 16px;
  padding: 6px 12px;
  background-color: #e0e0e0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.back-button:hover {
  background-color: #d0d0d0;
}

.back-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.current-path {
  font-size: 12px;
  color: #666;
  overflow: hidden;
  text-overflow: ellipsis;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1001;
}

.modal-content {
  background-color: white;
  padding: 24px;
  border-radius: 8px;
  min-width: 300px;
  max-width: 90%;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.modal-content h3 {
  margin-top: 0;
  margin-bottom: 16px;
  color: #333;
}

.modal-content input {
  width: 100%;
  padding: 10px;
  margin: 10px 0;
  box-sizing: border-box;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.modal-content input:focus {
  outline: none;
  border-color: #4CAF50;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.modal-actions button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.modal-actions button:not(.danger) {
  background-color: #f0f0f0;
}

.modal-actions button:not(.danger):hover {
  background-color: #e0e0e0;
}

.modal-actions button.danger {
  background-color: #f44336;
  color: white;
}

.modal-actions button.danger:hover {
  background-color: #d32f2f;
}

.preview-modal .modal-content {
  max-width: 90%;
  max-height: 90vh;
  width: 800px;
  display: flex;
  flex-direction: column;
}


.file-name-input label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #666;
}

.file-name-input input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

@media (max-width: 768px) {

  .side-menu {
    width: 100%;
    margin-right: 0;
    margin-bottom: 0;
    position: static;
  }

  .side-menu-section {
    display: inline-block;
    margin-right: 20px;
    margin-bottom: 0;
  }

  .file-list-section {
    width: 100%;
  }

  .action-panel {
    width: 100%;
    position: static;
    margin-top: 16px !important;
  }

  .file-list {
    max-width: 100%;
  }

  .action-panel {
    width: 100%;
    position: static;
    margin-top: 16px;
  }

  .toolbar {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .left-actions {
    width: 100%;
    justify-content: space-between;
  }

  .navigation {
    width: 100%;
  }

  .preview-modal .modal-content {
    width: 95%;
    height: 95vh;
  }

}

/* 上传文件Btn */
.upload-file-btn {
  background: #c5e8c5;
}

.file-icon {
  fill: #607D8B; /* 蓝色灰色 */
}

/* SVG图标样式 */
.icon svg {
  width: 20px;
  height: 20px;
  margin-right: 8px;
  flex-shrink: 0;
}

.folder-icon {
  fill: #FFA000; /* 文件夹图标颜色 - 橙色 */
}


/* 调整列表项对齐 */
.file-list li {
  display: flex;
  align-items: center;
}

/* 调整名称部分 */
.name {
  flex: 1;
  min-width: 0; /* 防止flex item溢出 */
}

/* 图标样式 */
.icon img {
  width: 20px;
  height: 20px;
  margin-right: 8px;
  flex-shrink: 0;
}
</style>